
### -------------------------------------------------- 
### ---- General Set Up ---- 
### -------------------------------------------------- 

### Clear global environment
rm(list=ls()) 

### Libraries:
library(pacman)
p_load(tidyverse, readstata13, readr, magrittr, here)


### File Location (R project only)
here::i_am("Survey and App Match/104_match_tfa_to_survey.R")
file_path <- here()


### -------------------------------------------------- 
### ---- Download TFA Data ----
### -------------------------------------------------- 

### Download Data
tfa_dat <- read.dta13(paste0(file_path,"/Data/tfa_data.dta")) 


### -------------------------------------------------- 
### ---- Merge in New State Variable ----
### -------------------------------------------------- 

### Rename 
tfa_dat %<>% dplyr::rename(state_survey = state)

### Save as character (for merge w/ abbreviations)
tfa_dat %<>% dplyr::mutate(state_survey=as.character(state_survey))

### Open data with abbreviations
data(state)

### Save abbreviations and full names as data frame 
abb <- data.frame("state_st" = state.abb, 
                  "state_survey" = state.name) %>%
  dplyr::mutate_all(as.character) #as character
abb %<>% rbind(., c("DC", "District of Columbia")) #add dc 

### Merge abbreviations with TFA dataset 
tfa_dat %<>% dplyr::left_join(x=., y=abb, by=c("state_survey"))


### -------------------------------------------------- 
### ---- Saved Matched PhysicalZipCode Data  ---- 
### -------------------------------------------------- 

tfa_dat_zipcodematch_FINAL <- tfa_dat

save(tfa_dat_zipcodematch_FINAL, 
     file=paste0(file_path,"/Survey and App Match/Temp_Data/tfa_to_state_survey.RData")) #changed name 


